package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.market;

import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.supplier.Supplier;
import com.gzhryc.net_marketing.supplier.SupplierManager;
import com.gzhryc.net_marketing.supplier.erp.dao.db.SupplierGoodsSpace;
import com.gzhryc.net_marketing.supplier.erp.dao.enums.SupplierGoodsEnum;
import com.gzhryc.net_marketing.supplier.erp.services.SupplierGoodsSpaceService;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class GoodsSpaceController {

	static Logger log = Logger.getLogger(GoodsSpaceController.class);

	@Route(value = "/market/goods/space/list.data")
	public ListResult<SupplierGoodsSpace> listData(HttpServletRequest request) {
		ListResult<SupplierGoodsSpace> result = new ListResult<SupplierGoodsSpace>(DtoConstants.NULL_DATA, "暂无数据");
		Supplier supplier = SupplierManager.getSupplier();
		Long goodsId = WebUtils.getLong(request,"goodsId");
		if(goodsId != null && supplier != null) {
			List<SupplierGoodsSpace> data = SupplierGoodsSpaceService.self(supplier.getJdbcKey()).findByGoodsId(goodsId);
			if (data != null && data.size() > 0) {
				result.setCode(DtoConstants.SUCCESS,null);
				result.getBody().setData(data);
				String resDomain = PropertyTools.get().getString("resDomain", "");
				for (SupplierGoodsSpace goodsSpace : result.getBody().getData()) {
					if (StringUtils.isNotBlank(goodsSpace.getThumbnailImg()) && !goodsSpace.getThumbnailImg().startsWith("http")) {
						goodsSpace.setThumbnailImg(resDomain + goodsSpace.getThumbnailImg());
					}
				}
			}
		}
		return result;
	}

	@Route(value = { "/market/goods/space/add.html" })
	public String add(HttpServletRequest request) {
		request.setAttribute("stateList", SupplierGoodsEnum.State.values());
		return "/market/goods/space.html";
	}

	@Route(value = { "/market/goods/space/add.do" })
	public AjaxResult doAdd(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增商品规格失败");
		Supplier supplier = SupplierManager.getSupplier();
		SupplierGoodsSpace goodsSpace = WebUtils.getParameter(request, SupplierGoodsSpace.class);
		try{
			if (SupplierGoodsSpaceService.self(supplier.getJdbcKey()).add(goodsSpace)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Route(value = { "/market/goods/space/update.html" })
	public String update(HttpServletRequest request) {
		Supplier supplier = SupplierManager.getSupplier();
		Long goodsId = WebUtils.getLong(request,"goodsId");
		Long id = WebUtils.getLong(request, "id");
		if (goodsId != null && id != null) {
			SupplierGoodsSpace goodsSpace = SupplierGoodsSpaceService.self(supplier.getJdbcKey()).getByKeys(goodsId,id);
			if (goodsSpace != null) {
				WebUtils.setAttribute(request, goodsSpace);
			}
		}
		request.setAttribute("stateList", SupplierGoodsEnum.State.values());
		return "/market/goods/space.html";
	}

	@Route(value = { "/market/goods/space/update.do" })
	public AjaxResult doUpdate(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改商品规格失败");
		Supplier supplier = SupplierManager.getSupplier();
		SupplierGoodsSpace goodsSpace = WebUtils.getParameter(request, SupplierGoodsSpace.class);
		if (goodsSpace.getGoodsId() != null && goodsSpace.getId() != null) {
			try {
				if(SupplierGoodsSpaceService.self(supplier.getJdbcKey()).updateNotNull(goodsSpace)){
					result.setCode(DtoConstants.SUCCESS,null);
				}
			} catch (LogicException e) {
				result.setMsg(e.getMessage());
			}
		}
		return result;
	}

	@Route(value = { "/market/goods/space/del.do" })
	public AjaxResult doDel(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除商品规格失败");
		Supplier supplier = SupplierManager.getSupplier();
		Long goodsId = WebUtils.getLong(request,"goodsId");
		Long id = WebUtils.getLong(request, "id");
		if (goodsId != null && id != null) {
			try {
				if (SupplierGoodsSpaceService.self(supplier.getJdbcKey()).deleteByKeys(goodsId,id)) {
					result.setCode(DtoConstants.SUCCESS, null);
				}
			} catch (LogicException e) {
				result.setMsg(e.getMessage());
			}
		}
		return result;
	}
}
